﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using YDModel;
using YDServices.Comm;
using static YDServices.Enum.OrderEnum;

namespace YDServices
{
    public class EmbryoService : BaseService
    {

        //int ordertype = (int)OrderTyp.胚布;
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="conds"></param>
        /// <returns></returns>
        public List<T_Order> getList(ConditionItem[] conds)
        { 
             try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var list = db.T_Order.Where(o=>o.State!=false).AsQueryable();
                    if (conds != null)
                    {
                        var qb = QueryBuilder<T_Order>.Create().AddConditions(conds);
                        list = list.Where(qb.ToExpression());
                    }
                    return list.ToList();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="tu"></param>
        public void AddItem(T_Order tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                { 

                    tu.State = true;
                    tu.CreatedTime = GetSqlNow();
                    tu.CreatedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    db.T_Order.Add(tu);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tu"></param>
        public void EditItem(T_Order tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var item = db.T_Order.Where(o => o.ID == tu.ID && o.State == true).FirstOrDefault();
                    if (item == null)
                    {
                        throw new ApplicationException("胚布信息不存在!");
                    }
                    item.Factory_ID = tu.Factory_ID;
                    item.Factory_Name = tu.Factory_Name;
                    item.OperatintTime = tu.OperatintTime;
                    item.Quantity = tu.Quantity;
                    item.Weight = tu.Weight;
                    item.SumWeight = tu.SumWeight;
                    item.SerialNumber = tu.SerialNumber;
                    item.Spec_ID = tu.Spec_ID;
                    item.Spec_Name = tu.Spec_Name;
                    item.UnitPrice = tu.UnitPrice;
                    item.TotalPrice = tu.TotalPrice;
                    //item.IsSanding = tu.IsSanding;
                    item.ModifiedBy = "["+Global.loginUser.ID+"]"+ Global.loginUser.UserName;
                    item.ModifiedTime = GetSqlNow();  

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tu"></param>
        /// <param name="loginUser"></param>
        public void DelItem(T_Order item)  //List<T_Order> tuList 
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    
                    var fact = db.T_Order.Where(o => o.ID == item.ID ).FirstOrDefault();
                    if (fact == null)
                    {
                        throw new ApplicationException("胚布信息不存在!");
                    }
                    fact.State = false;
                    fact.DeletedTime = GetSqlNow();
                    fact.DeletedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName; 

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }
 

    }
}
